package com.dl698.parse;

import com.dl698.axdr.*;
import com.dl698.bean.FrameData;
import com.dl698.util.DataUtils;
import com.dl698.tools.ResultCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * ParseProxyResponse类
 * 实现代理响应解析功能
 * 
 * @author wind
 * @version 1.1.1
 */
public class ParseProxyResponse {

    public static String parseProxyGetResponseList(SubSeqOfResult subSeqOfResult) {
        FrameData frameData = new FrameData();
        Map<String, Object> seqProxyList = new HashMap<>();
        Map<String, Object> seqProxyData = new HashMap<>();
        ArrayList<Object> proxyList = new ArrayList<>();

        for (int i = 0; i < subSeqOfResult.size(); i++) {
            ArrayList<Object> proxyData = new ArrayList<>();

            // 修改这里的逻辑，直接访问单个Get_Result对象
            Get_Result result = subSeqOfResult.get(i).getResult();
            switch (result.getChoiceIndex().getValue()) {
                case 0:
                    Map<String, Object> dar = new HashMap<>();
                    result.getDar().encodeAndSave(1000);
                    dar.put("dar", DataUtils.bytesToHexString(
                            result.getDar().getDataCode(),
                            0,
                            result.getDar().getDataCode().length));
                    proxyData.add(dar);
                    break;
                case 1:
                    String value = ParseTools.parseData(result.getData());
                    proxyData.add(value);
                    break;
            }

            seqProxyData.put("seq_of_data", proxyData);
            proxyList.add(seqProxyData);
        }

        seqProxyList.put("seq_proxy_list", proxyList);
        frameData.setData(seqProxyList);
        frameData.setCode(ResultCode.SUCCESS);
        return frameData.toJsonString();
    }

    public static String parseProxyGetRecord(ProxyGetResponseRecord proxyGetResponseRecord) {
        FrameData frameData = new FrameData();

        // 修改这里，使用getResult()而不是getRespData()
        SubChoiceRespData respData = proxyGetResponseRecord.getResult().getResult();
        switch (respData.getChoiceIndex().getValue()) {
            case 0:
                Map<String, Object> dar = new HashMap<>();
                respData.getDar().encodeAndSave(1000);
                dar.put("dar", DataUtils.bytesToHexString(
                        respData.getDar().getDataCode(),
                        0,
                        respData.getDar().getDataCode().length));
                frameData.setData(dar);
                break;
            case 1:
                Map<String, Object> seq_of_a_record = new HashMap<>();
                Map<String, Object> a_record = new HashMap<>();
                ArrayList<Object> seq_a_record_list = new ArrayList<>();

                // 修改这里，使用getRecords()而不是getRespData().getRecords()
                SubSeqOfRecords records = respData.getRecords();
                for (int i = 0; i < records.size(); i++) {
                    Map<String, Object> seq_data = new HashMap<>();
                    ArrayList<String> seq_data_list = new ArrayList<>();

                    for (int j = 0; j < records.get(i).getData().size(); j++) {
                        String value = ParseTools.parseData(records.get(i).getData().get(j));
                        seq_data_list.add(value);
                    }

                    a_record.put("a_record", seq_data_list);
                    seq_data.put("seq_of_data", seq_data_list);
                    seq_a_record_list.add(a_record);
                }

                seq_of_a_record.put("seq_of_a_record", seq_a_record_list);
                frameData.setData(seq_of_a_record);
                break;
        }

        frameData.setCode(ResultCode.SUCCESS);
        return frameData.toJsonString();
    }

    public static String parseProxyTransCommand(ProxyTransCommandResponse proxyTransCommandResponse) {
        FrameData frameData = new FrameData();
        Map<String, Object> transResult = new HashMap<>();

        // 修改这里，直接使用getChoiceIndex()返回的long值
        switch ((int) proxyTransCommandResponse.getResult().getChoiceIndex()) {
            case 0:
                Map<String, Object> dar = new HashMap<>();
                proxyTransCommandResponse.getResult().getDar().encodeAndSave(1000);
                dar.put("dar", DataUtils.bytesToHexString(
                        proxyTransCommandResponse.getResult().getDar().getDataCode(),
                        0,
                        proxyTransCommandResponse.getResult().getDar().getDataCode().length));
                transResult.put("transResult", dar);
                break;
            case 1:
                Map<String, Object> oct_string = new HashMap<>();
                oct_string.put("oct_string", proxyTransCommandResponse.getResult().getRetResult().toString());
                transResult.put("transResult", oct_string);
                break;
        }

        frameData.setData(transResult);
        frameData.setCode(ResultCode.SUCCESS);
        return frameData.toJsonString();
    }

    public static String parseProxySetResponseNormalList(ProxySetResponseList proxySetNormalList) {
        FrameData frameData = new FrameData();
        Map<String, Object> proxySetResponseList = new HashMap<>();
        ArrayList<Object> darArray = new ArrayList<>();

        for (int i = 0; i < proxySetNormalList.getResult().size(); i++) {
            Map<String, Object> proxySet = new HashMap<>();
            ArrayList<Object> darList = new ArrayList<>();
            ProxySetResponse proxySetItem = proxySetNormalList.getResult().get(i);

            for (int j = 0; j < proxySetItem.getResult().size(); j++) {
                Map<String, Object> dar = new HashMap<>();
                ProxySet setOfResult = proxySetItem.getResult().get(j);
                setOfResult.getDar().encodeAndSave(1000);
                dar.put("dar", DataUtils.bytesToHexString(
                        setOfResult.getDar().getDataCode(),
                        0,
                        setOfResult.getDar().getDataCode().length));
                darList.add(dar);
            }

            proxySet.put("seq_dar", darList);
            darArray.add(proxySet);
        }

        frameData.setCode(ResultCode.SUCCESS);
        proxySetResponseList.put("seq_of_dar", darArray);
        frameData.setData(proxySetResponseList);
        return frameData.toJsonString();
    }

    public static String parseProxySetThenGetResponseList(ProxySetThenGetResponseList proxySetThenGet) {
        FrameData frameData = new FrameData();
        Map<String, Object> proxySetThenGetList = new HashMap<>();
        ArrayList<Object> resultArray = new ArrayList<>();

        for (int i = 0; i < proxySetThenGet.getResult().size(); i++) {
            ProxySetThenGetResponse proxySetThenGetResponse = proxySetThenGet.getResult().get(i);
            ArrayList<Object> result = new ArrayList<>();
            Map<String, Object> proxySetThenGetMap = new HashMap<>();

            for (int j = 0; j < proxySetThenGetResponse.getResult().size(); j++) {
                Map<String, Object> setDar = new HashMap<>();
                Map<String, Object> getResult = new HashMap<>();

                // 修复类型转换错误：使用ProxySetThenGet而不是ProxySetThenGetResult
                ProxySetThenGet setThenGet = proxySetThenGetResponse.getResult().get(j);
                setThenGet.getDar().encodeAndSave(1000);
                setDar.put("dar", DataUtils.bytesToHexString(
                        setThenGet.getDar().getDataCode(),
                        0,
                        setThenGet.getDar().getDataCode().length));

                // 修复类型转换错误：使用Get_Result而不是ProxySetThenGetGetResult
                Get_Result get_result = setThenGet.getGetResult();
                switch (get_result.getChoiceIndex().getValue()) {
                    case 0:
                        Map<String, Object> dar = new HashMap<>();
                        get_result.getDar().encodeAndSave(1000);
                        dar.put("dar", DataUtils.bytesToHexString(
                                get_result.getDar().getDataCode(),
                                0,
                                get_result.getDar().getDataCode().length));
                        getResult.put("dar", dar);
                        break;
                    case 1:
                        getResult.put("data", ParseTools.parseData(get_result.getData()));
                        break;
                }

                setDar.put("get_result", getResult);
                result.add(setDar);
            }

            proxySetThenGetMap.put("seq_result", result);
            resultArray.add(proxySetThenGetMap);
        }

        proxySetThenGetList.put("seq_of_result", resultArray);
        frameData.setCode(ResultCode.SUCCESS);
        frameData.setData(proxySetThenGetList);
        return frameData.toJsonString();
    }

    public static String parseProxyActionResponseList(ProxyActionResponseList proxyActionList) {
        FrameData frameData = new FrameData();
        Map<String, Object> proxyActionResponseList = new HashMap<>();
        ArrayList<Object> resultArray = new ArrayList<>();

        for (int i = 0; i < proxyActionList.getResult().size(); i++) {
            Map<String, Object> proxyActionResponse = new HashMap<>();
            ArrayList<Object> resultList = new ArrayList<>();
            // 修复类型转换错误：使用ProxyActionResponse而不是ProxyActionResponseListItem
            ProxyActionResponse proxyAction = proxyActionList.getResult().get(i);

            for (int j = 0; j < proxyAction.getResult().size(); j++) {
                Map<String, Object> actionResult = new HashMap<>();
                Map<String, Object> dataOption = new HashMap<>();

                // 修复类型转换错误：使用RETOMDDATA而不是ProxyActionResult
                RETOMDDATA actionList = proxyAction.getResult().get(j);
                actionList.getDar().encodeAndSave(1000);
                // 修改这里，正确获取Data对象
                if (actionList.getRetData() != null && actionList.getRetData().isUsed()) {
                    // 修复类型错误：getRetData().getValue()返回的是Data类型，不是RETOMDDATA类型
                    Data data = actionList.getRetData().getValue();
                    dataOption.put("data", ParseTools.parseData(data));
                }
                actionResult.put("dar", DataUtils.bytesToHexString(
                        actionList.getDar().getDataCode(),
                        0,
                        actionList.getDar().getDataCode().length));
                actionResult.put("data", dataOption);
                resultList.add(actionResult);
            }

            proxyActionResponse.put("seq_action_result", resultList);
            resultArray.add(proxyActionResponse);
        }

        proxyActionResponseList.put("seq_result", resultArray);
        frameData.setCode(ResultCode.SUCCESS);
        frameData.setData(proxyActionResponseList);
        return frameData.toJsonString();
    }

    public static String parseProxyActionThenGetResponseList(ProxyActionThenGetResponseList proxyActionThenGetList) {
        FrameData frameData = new FrameData();
        Map<String, Object> proxyActionThenGetListMap = new HashMap<>();
        ArrayList<Object> resultArray = new ArrayList<>();

        for (int i = 0; i < proxyActionThenGetList.getResult().size(); i++) {
            // 修复类型转换错误：使用ProxyActionThenGetResponse而不是ProxyActionThenGetResponseListItem
            ProxyActionThenGetResponse proxyActionThenGet = proxyActionThenGetList.getResult().get(i);
            Map<String, Object> proxyActionThenGetMap = new HashMap<>();
            ArrayList<Object> resultList = new ArrayList<>();

            for (int j = 0; j < proxyActionThenGet.getResult().size(); j++) {
                Map<String, Object> actionThenGetResult = new HashMap<>();
                Map<String, Object> dataOption = new HashMap<>();

                // 修复类型转换错误：使用READRETOMDDATA而不是ProxyActionThenGetResult
                READRETOMDDATA actionThenGetItem = proxyActionThenGet.getResult().get(j);
                actionThenGetItem.getDar().encodeAndSave(1000);
                // 修改这里，正确获取Data对象
                if (actionThenGetItem.getRetData() != null && actionThenGetItem.getRetData().isUsed()) {
                    // 修复类型错误：getRetData().getValue()返回的是Data类型，不是RETOMDDATA类型
                    Data data = actionThenGetItem.getRetData().getValue();
                    dataOption.put("data", ParseTools.parseData(data));
                }
                actionThenGetResult.put("dar", DataUtils.bytesToHexString(
                        actionThenGetItem.getDar().getDataCode(),
                        0,
                        actionThenGetItem.getDar().getDataCode().length));
                actionThenGetResult.put("data", dataOption);
                resultList.add(actionThenGetResult);
            }

            proxyActionThenGetMap.put("seq_of_action_then_get_result", resultList);
            resultArray.add(proxyActionThenGetMap);
        }

        frameData.setCode(ResultCode.SUCCESS);
        proxyActionThenGetListMap.put("seq_of_result", resultArray);
        frameData.setData(proxyActionThenGetListMap);
        return frameData.toJsonString();
    }
}